package org.example.algorithm.dp;

//309. 买卖股票的最佳时机含冷冻期
/**
示例 1:
输入: prices = [1,2,3,0,2]
输出: 3
解释: 对应的交易状态为: [买入, 卖出, 冷冻期, 买入, 卖出]
 */
public class MaxProfitHaveFrozen {

    //dp[i][1] = max(dp[i-1][1], dp[i-2][0]-prices[i])
    //dp[i][0] = max(dp[i-1][0], dp[i-1][1]+prices[i])
    public int maxProfit(int[] prices) {
        int len = prices.length;
        int[][] dp = new int[len+1][2];
        dp[0][0] = 0;
        dp[0][1] = Integer.MIN_VALUE;
        for (int i=0;i<len;i++) {
            if (i >= 1) {
                dp[i+1][1] = Math.max(dp[i][1], dp[i-1][0]-prices[i]);
            } else {
                dp[i+1][1] = Math.max(dp[i][1], -prices[i]);
            }
            dp[i+1][0] = Math.max(dp[i][0], dp[i][1]+prices[i]);
        }
        return dp[len][0];
    }
}
